package com.iwhalecloud.bss.kite.cucc.service.scene.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosAttrDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosOrderDTO;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class CuccSceneUtil {

    /**
     * 根据工单信息判断是否为核查转定单
     * @param orderId
     * @param kitePosDTO
     * @return
     */
    public static boolean isTransferOrderSceneByWorkSheetInfo(String orderId, KitePosDTO kitePosDTO) {
        KitePosOrderDTO order = kitePosDTO.getOrder();
        String bizType = order.getBizType();
        String upstreamOrderId = order.getUpstreamOrderId();
        return "1001".equals(bizType) && KiteStringUtils.isNotEmpty(upstreamOrderId) && !KiteStringUtils.equals(orderId, upstreamOrderId);
    }

    /**
     * 根据工单信息判断是否为核查转定单
     * @param orderId
     * @param kitePosDTO
     * @return
     */
    public static boolean isModifyTransferOrderSceneByWorkSheetInfo(String orderId, KitePosDTO kitePosDTO ,String serviceOfferId) {
        KitePosOrderDTO order = kitePosDTO.getOrder();
        String upstreamOrderId = order.getUpstreamOrderId();
        return KiteStringUtils.equals(serviceOfferId,"2826") && KiteStringUtils.isNotEmpty(upstreamOrderId) && !KiteStringUtils.equals(orderId, upstreamOrderId);
    }

    /**
     * 获取map中第一个数据值，并从map删掉
     *
     * @param map 数据源
     * @return key 属性组实例ID
     * @return value 属性组的属性列表
     */
    public static Map<String, Object> getOneAndRemove(Map<String, List<KitePosAttrDTO>> map) {
        Map<String, Object> result = new HashMap<>();
        for (Map.Entry<String, List<KitePosAttrDTO>> entry : map.entrySet()) {
            List<KitePosAttrDTO> attrs = entry.getValue();
            if (attrs != null) {
                result.put("key", entry.getKey());
                result.put("value", attrs);
                map.remove(entry.getKey());
                break;
            }
        }
        return result;
    }

    public static Map<String, List<KitePosAttrDTO>> getSameFeePlanAttrGroups(String fieldName, String feePlanInstId, List<KitePosAttrDTO> attrGroups) {
        Map<String, List<KitePosAttrDTO>> result = new HashMap<>();
        for (KitePosAttrDTO attrGroup : attrGroups) {
            Map<String, Object> attrValueMap = JSONObject.parseObject(attrGroup.getValue(), Map.class);
            Iterator<String> it = attrValueMap.keySet().iterator();

            while (it.hasNext()) {
                String attrGroupInstId = it.next();
                JSONArray attrListValue = (JSONArray) attrValueMap.get(attrGroupInstId);
                List<KitePosAttrDTO> prodInstAttrs = JSONArray.parseArray(JSONArray.toJSONString(attrListValue), KitePosAttrDTO.class);
                if (prodInstAttrs.stream().anyMatch(attr -> KiteStringUtils.equals(fieldName, attr.getCode()) && KiteStringUtils.equals(feePlanInstId, attr.getValue()))) {
                    result.put(attrGroupInstId, prodInstAttrs);
                }
            }
        }
        return result;
    }
}
